package com.dys.teststringbuilder10;

/**
 * @className: com.dys.teststringbuilder10.StringBuilderTest
 * @description: com.dys.teststringbuilder10
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/07/29 下午9:07
 */
public class StringBuilderTest {
    /**
     * 这是一个main()方法,是应用程序的入口
     *
     * @param args 方法的形参列表,是一个字符串数组,包含了从命令行传递给程序的参数
     */
    public static void main(String[] args) {
        new StringBuilderTest().testStringBufferMethod();
    }

    public void testStringBuilder() {
        // 表面上调用StringBuilder的空构造器，实际底层是对value数组进行初始化，长度为16
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder(3);
        StringBuilder sb3 = new StringBuilder("abc");
        // 链式调用,return this
        sb3 = sb3.append("DEF").append("GHI").append("JKL").append("oooooooooooo");
        System.out.println(sb3);
        // 可变字符串
        StringBuilder sb4 = new StringBuilder();
        System.out.println(sb4.append("abc") == sb4.append("def"));
    }

    /**
     * StringBuilder常用方法
     */
    public void testStringBuilderMethod() {
        StringBuilder sb1 = new StringBuilder("abcdefghijklmnopqrst");
        // 增
        sb1.append("ABC");
        System.out.println(sb1);
        // 删 删除位置在[0,3)上的字符 abc
        sb1.delete(0, 3);
        System.out.println(sb1);
        // 删除指定位置的字符: 从e开始
        sb1.deleteCharAt(0);
        System.out.println(sb1);

        // 改：-->插入
        StringBuilder sb2 = new StringBuilder("abcdefghijklmnopqrst");
        sb2.insert(3, ',');
        System.out.println(sb2);

        // 改：-->替换
        StringBuilder sb3 = new StringBuilder("abcdefghijklmnopqrst");
        // 将abc替换成@符号
        // sb3.replace(0,3, "@");
        // 替换2
        sb3.setCharAt(0, '@');
        System.out.println(sb3);

        // 查
        StringBuilder sb4 = new StringBuilder("abcdefghijklmnopqrst");
        for (int i = 0; i < sb4.length(); i++) {
            System.out.print(sb4.charAt(i) + "\t");
        }

        System.out.println();
        // 截取 截取[2,4)返回的是一个新的String，对StringBuilder没有影响
        String substring = sb4.substring(2, 4);
        System.out.println(substring);
        System.out.println(sb4);
    }

    /**
     * StringBuffer常用方法
     */
    public void testStringBufferMethod() {
        StringBuffer sb1 = new StringBuffer("abcdefghijklmnopqrst");
        // 增
        sb1.append("ABC");
        System.out.println(sb1);
        // 删 删除位置在[0,3)上的字符 abc
        sb1.delete(0, 3);
        System.out.println(sb1);
        // 删除指定位置的字符: 从e开始
        sb1.deleteCharAt(0);
        System.out.println(sb1);

        // 改：-->插入
        StringBuilder sb2 = new StringBuilder("abcdefghijklmnopqrst");
        sb2.insert(3, ',');
        System.out.println(sb2);

        // 改：-->替换
        StringBuilder sb3 = new StringBuilder("abcdefghijklmnopqrst");
        // 将abc替换成@符号
        // sb3.replace(0,3, "@");
        // 替换2
        sb3.setCharAt(0, '@');
        System.out.println(sb3);

        // 查
        StringBuilder sb4 = new StringBuilder("abcdefghijklmnopqrst");
        for (int i = 0; i < sb4.length(); i++) {
            System.out.print(sb4.charAt(i) + "\t");
        }

        System.out.println();
        // 截取 截取[2,4)返回的是一个新的String，对StringBuilder没有影响
        String substring = sb4.substring(2, 4);
        System.out.println(substring);
        System.out.println(sb4);
    }
}
